package com.ruoyi.common.tools.excel.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.ruoyi.common.core.service.DictService;
import com.ruoyi.common.tools.constant.ERecordConstant;
import com.ruoyi.common.tools.enums.DeviceTypeEnum;
import com.ruoyi.common.tools.excel.base.MyExcelListener;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.erecord.device.domain.vo.DeviceImportDetailVo;
import com.ruoyi.erecord.device.service.IDeviceService;
import com.ruoyi.erecord.employee.service.IEmployeeService;
import com.ruoyi.erecord.vehicles.service.IVehiclesService;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;


/**
 * 人员 excel 导入
 *
 * @author cc
 */
public class DeviceExcelListener extends MyExcelListener<DeviceImportDetailVo> {

    private final DictService dictService = SpringUtils.getBean(DictService.class);
    private final IEmployeeService employeeService = SpringUtils.getBean(IEmployeeService.class);
    private final IVehiclesService vehiclesService = SpringUtils.getBean(IVehiclesService.class);

    private final IDeviceService iDeviceService = SpringUtils.getBean(IDeviceService.class);


    private String batchNo = null;

    public DeviceExcelListener(String batchNo) {
        this.batchNo = batchNo;
    }

    @Override
    public void doValidate(DeviceImportDetailVo data, AnalysisContext context) {
        //1.设置批次号
        //2.校验数据 与 匹配数据字典数据 设置对应字段
        //3.设置 处理状态 与 处理信息

        String code = Optional.of(data).map(DeviceImportDetailVo::getCode).orElse("0");
        data.setBatchNo(this.batchNo);

        int count = iDeviceService.queryByCode(code);
        List<String> validateList = new ArrayList<>();
        if (count > 0) {
            validateList.add(" 设备编码[".concat(code).concat("] 已存在！"));
        }
        if (!StringUtils.isEmpty(data.getStatusImport())) {
            String val = this.dictService.getDictValue("device_status", data.getStatusImport());
            if (!Optional.ofNullable(val).isPresent()) {
                validateList.add("[ 设备状态 ] 匹配不到数据");
            } else {
                data.setStatus(val);
            }
        } else {
            validateList.add("[ 设备状态 ] 为空");
        }

        if (!StringUtils.isEmpty(data.getTypeImport())) {
            String val = this.dictService.getDictValue("device_type", data.getTypeImport());
            if (!Optional.ofNullable(val).isPresent()) {
                validateList.add("[ 设备类型 ] 匹配不到数据");
            } else {
                data.setType(val);
            }
        } else {
            validateList.add("[ 设备类型 ] 为空");
        }
        //查询用户信息
        if (StringUtils.isEmpty(data.getName())) {
            validateList.add("[ 人名/车牌号 ] 为空");
        } else {
            //employeeService
            if (!Optional.ofNullable(data.getType()).isPresent()) {
                validateList.add("[ ] 为空");
            } else {
                Long id = DeviceTypeEnum.CAR.getCode().equals(data.getType())
                    ? vehiclesService.getVehiclesIdByNumber(data.getName())
                    : employeeService.getEmployeeIdByName(data.getName());
                if (!Optional.ofNullable(id).isPresent()) {
                    validateList.add("[ 人名/车牌 ] 匹配不到数据");
                } else {
                    data.setDeviceId(id);
                }
            }
        }

        if (!validateList.isEmpty()) {
            String errMsg = String.join(" ，", validateList);
            excelResult.getValidateList().add(errMsg);

            data.setMessage(errMsg);
            data.setImportState(ERecordConstant.COMMON_STATUS_ERROR);
        } else {
            data.setMessage("成功");
            data.setImportState(ERecordConstant.COMMON_STATUS_SUCCESS);
        }
    }

    @Override
    protected void doOnExceptionProcess(String errorMsg) {
        DeviceImportDetailVo vo = new DeviceImportDetailVo();
        vo.setBatchNo(this.batchNo);
        vo.setStatus(ERecordConstant.COMMON_STATUS_ERROR + "");
        vo.setMessage(errorMsg);
        this.excelResult.getList().add(vo);
    }
}
